﻿using System;
using System.Collections;
using System.Reflection;
using SimpleORM;

public static class IEnumerableExtension
{
    public static IList Clone(this IList source)
    {
        if (source == null)
            return null;

        MethodInfo mi = source.GetType().GetMethod("Clone");
        if (mi != null)
            return (IList)mi.Invoke(source, null);

        IList list = Activator.CreateInstance(source.GetType()) as IList;
        MethodInfo addMethod = list.GetType().GetMethod("Add");

        foreach (object item in source)
            addMethod.Invoke(list, new object[] { CDObject.TryClone(item) });

        return list;
    }
}
